﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;

namespace AlterGear.SQLLibrary
{
    [Serializable]
    public class SQLListSaved
    {
        private const string ISOLATED_STORAGEFILE = "SQLIndexer_SavedConnections.xml";

        [XmlArray]
        public List<SQLList> ListSaved { get; set; }
        public SQLListSaved()
        {
            this.ListSaved = new List<SQLList>();
        }

        public static SQLListSaved Deserailize()
        {
            SQLListSaved sqlListSaved = new SQLListSaved();

            using (IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream(ISOLATED_STORAGEFILE, FileMode.OpenOrCreate, FileAccess.Read))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(SQLListSaved));
                if (isoFileStream.Length > 0)
                {
                    using (TextReader textReader = new StreamReader(isoFileStream))
                    {
                        sqlListSaved = (SQLListSaved)xmlSerializer.Deserialize(textReader);
                    }
                }
            }
            return sqlListSaved;
        }

        public static void Serialize(SQLListSaved sqlListSaved)
        {
            using (IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream(ISOLATED_STORAGEFILE, FileMode.Truncate, FileAccess.Write))
            {
                
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(SQLListSaved));
                using (TextWriter textWriter = new StreamWriter(isoFileStream))
                {
                    xmlSerializer.Serialize(textWriter, sqlListSaved);
                }
            }
        }

        public static void SaveSQLList(SQLList sqlList)
        {
            if (!string.IsNullOrEmpty(sqlList.SQLServerName)
                && !string.IsNullOrEmpty(sqlList.SQLDatabaseName))
            {
                bool recFound = false;
                SQLListSaved sqlListSave = SQLListSaved.Deserailize();
                if (sqlListSave == null || sqlListSave.ListSaved.Count == 0)
                {
                    if (sqlListSave == null)
                        sqlListSave = new SQLListSaved();
                    sqlListSave.ListSaved.Add(sqlList);
                }
                else
                {
                    for (int i = 0; i < sqlListSave.ListSaved.Count; i++)
                    {
                        SQLList locSqlList = sqlListSave.ListSaved[i];
                        if (locSqlList.CompareTo(sqlList) == 0)
                        {
                            sqlListSave.ListSaved[i] = sqlList;
                            recFound = true;
                            break;
                        }
                    }
                    if (recFound == false)
                    {
                        sqlListSave.ListSaved.Add(sqlList);
                    }
                }
                SQLListSaved.Serialize(sqlListSave);
            }
        }
        public static void DeleteSQLList(SQLList sqlList)
        {
            SQLListSaved sqlListSave = SQLListSaved.Deserailize();
            if (!(sqlListSave == null || sqlListSave.ListSaved.Count == 0))
            {
                for (int i = 0; i < sqlListSave.ListSaved.Count; i++)
                {
                    SQLList locSqlList = sqlListSave.ListSaved[i];
                    if (locSqlList.CompareTo(sqlList) == 0)
                    {
                        sqlListSave.ListSaved.RemoveAt(i);
                        break;
                    }
                }
            }
            SQLListSaved.Serialize(sqlListSave);
        }

        public static bool ContainsSQLList(SQLList sqlList)
        {
            bool ret = false;
            SQLListSaved sqlListSave = SQLListSaved.Deserailize();
            if (!(sqlListSave == null || sqlListSave.ListSaved.Count == 0))
            {
                for (int i = 0; i < sqlListSave.ListSaved.Count; i++)
                {
                    SQLList locSqlList = sqlListSave.ListSaved[i];
                    if (locSqlList.CompareTo(sqlList) == 0)
                    {
                        ret = true;
                        break;
                    }
                }
            }
            return ret;
        }
    }
}
